<template>
<div class="keyboard_wrap">
  <div class="keyboard black" @click.stop="_handleKeyPress">
    <div class="key-row">
      <a class="key-cell" data-num="1">1</a>
      <a class="key-cell" data-num="2">2</a>
      <a class="key-cell" data-num="3">3</a>
    </div>
    <div class="key-row">
      <a class="key-cell" data-num="4">4</a>
      <a class="key-cell" data-num="5">5</a>
      <a class="key-cell" data-num="6">6</a>
    </div>
    <div class="key-row">
      <a class="key-cell" data-num="7">7</a>
      <a class="key-cell" data-num="8">8</a>
      <a class="key-cell" data-num="9">9</a>
    </div>
    <div class="key-row">
      <a class="key-cell" data-num="C">清空</a>
      <a class="key-cell" data-num="0">0</a>
      <a class="key-cell" data-num="S">确认</a>
    </div>
  </div>
</div>
</template>
<script>
import { Toast } from "vant";
export default {
  name: "Keyboard",
  data() {
    return {
      number: ""
    };
  },
  methods: {
    //处理按键
    _handleKeyPress(e) {
      let num = e.target.dataset.num;
      //不同按键处理逻辑
      // -1 代表无效按键，直接返回
      if (num == -1) return false;
      switch (String(num)) {
        //清空键
        case "C":
          this._handleClearKey();
          break;
        //确认键
        case "S":
          this._handleConfirmKey();
          break;
        default:
          this._handleNumberKey(num);
          break;
      }
    },
    //处理清空键
    _handleClearKey() {
      this.number = "";
      this.$emit("number", "");
    },
    //处理数字
    _handleNumberKey(num) {
      let S = this.number;
      this.number = S + num;
      this.$emit("number",this.number);
    },
    //提交
    _handleConfirmKey() {
      let S = this.number;
      //未输入
      if (!S.length) {
        Toast({
          message: "未输入",
          duration: 1500
        });
        return false;
      }
      this.$emit("confirm", S);
    }
  },
  watch: {
    number(newval, oldval) {
      this.$emit("number", newval);
    }
  }
};
</script>
<style lang="less" scoped>
.arrow_bottom{
  height: 20%;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top:1px solid #ccc;
}
.keyboard_wrap{
  position: fixed;
  top:0;
  left:0;
  right:0;
  bottom:0;
}
.black{color:#333}
a:active{
  background-color: #eee;
}
.keyboard {
  font-size: 16px;
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  right:0;
  height: 30vh;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
}
.keyboard .key-row {
  display: flex;
  display: -webkit-flex;
  position: relative;
  height: 25%;
}
.keyboard .key-row::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 1px;
  border-top: 1px solid #ccc;
  color: #d5d5d6;
}
.keyboard .key-cell {
  flex: 1;
  width: 33.33%;
  height: 100%;
  vertical-align: middle;
  -webkit-box-flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.keyboard .key-cell::after {
  content: "";
  position: absolute;
  overflow: hidden;
  top: 0;
  right: 0;
  bottom: 0;
  height: 100%;
  border-right: 1px solid #ccc;
  color: #d5d5d6;
}
.keyboard .key-cell:nth-last-child(1)::after {
  border-right: 0;
}
.keyboard .disabled {
  background: rgba(0, 0, 0, 0.2);
}
.keyboard .key-confirm {
  position: absolute;
  text-align: center;
  height: 25%;
  width: 25%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #fff;
  z-index: 5;
  right: 0;
  bottom: 0;
  box-sizing: border-box;
  border-top:1px solid #ccc;
}

</style>